package com.manlitech.cloudboot.config.openfeignconfig;/*
 *Created by Intellij IDEA.
 *User:  Shensg
 *Date:  2021/3/5
 */


import com.manlitech.cloudboot.common.constant.LogAndAuthConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Configuration
public class CustomFeignRequestInterceptor implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            // 转发token
            template.header(LogAndAuthConstant.TOKEN, request.getHeader(LogAndAuthConstant.TOKEN));
            template.header(LogAndAuthConstant.TRACEID, request.getHeader(LogAndAuthConstant.TRACEID));
        }
    }
}
